1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.util.concurrent;
18  
19  import java.util.concurrent.Callable;
20  import java.util.concurrent.Executor;
21  import java.util.concurrent.FutureTask;
22  
23  import javax.annotation.Nullable;
24  
25  /**
26   * A {@link FutureTask} that also implements the {@link ListenableFuture}
27   * interface.  Unlike {@code FutureTask}, {@code ListenableFutureTask} does not
28   * provide an overrideable {@link FutureTask#done() done()} method.  For similar
29   * functionality, call {@link #addListener}.
30   * 
31   * <p>
32   *
33   * @author Sven Mawson
34   * @since 1.0
35   */
36  public class ListenableFutureTask<V> extends FutureTask<V>
37      implements ListenableFuture<V> {
38    // TODO(cpovirk): explore ways of making ListenableFutureTask final. There are
39    // some valid reasons such as BoundedQueueExecutorService to allow extends but it
40    // would be nice to make it final to avoid unintended usage.
41  
42    // The execution list to hold our listeners.
43    private final ExecutionList executionList = new ExecutionList();
44  
45    /**
46     * Creates a {@code ListenableFutureTask} that will upon running, execute the
47     * given {@code Callable}.
48     *
49     * @param callable the callable task
50     * @since 10.0
51     */
52    public static <V> ListenableFutureTask<V> create(Callable<V> callable) {
53      return new ListenableFutureTask<V>(callable);
54    }
55  
56    /**
57     * Creates a {@code ListenableFutureTask} that will upon running, execute the
58     * given {@code Runnable}, and arrange that {@code get} will return the
59     * given result on successful completion.
60     *
61     * @param runnable the runnable task
62     * @param result the result to return on successful completion. If you don't
63     *     need a particular result, consider using constructions of the form:
64     *     {@code ListenableFuture<?> f = ListenableFutureTask.create(runnable,
65     *     null)}
66     * @since 10.0
67     */
68    public static <V> ListenableFutureTask<V> create(
69        Runnable runnable, @Nullable V result) {
70      return new ListenableFutureTask<V>(runnable, result);
71    }
72  
73    ListenableFutureTask(Callable<V> callable) {
74      super(callable);
75    }
76  
77    ListenableFutureTask(Runnable runnable, @Nullable V result) {
78      super(runnable, result);
79    }
80  
81    @Override
82    public void addListener(Runnable listener, Executor exec) {
83      executionList.add(listener, exec);
84    }
85  
86    /**
87     * Internal implementation detail used to invoke the listeners.
88     */
89    @Override
90    protected void done() {
91      executionList.execute();
92    }
93  }